<?php $paginator = $this->Paginator; ?>
<?php
if (isset($type) && sizeof($rows)) {

    $name_type = '';
    $text_display = '';
    switch ($type) {
        case 1:
            $name_type = 'Company';
            foreach ($rows as $row) :
                $text_display .='<div class="services-box services-bg1 e_bounce">'
                        . '<h5>' . $row["Company"]["name"] . '</h5>'
                        . '<p>'
                        . 'Total Broker : ' . $row["Company"]["brokers"] . '<br>'
                        . '</p>'
                        . '<p>'
                        . $row['Company']['short_description']
                        . '</p>'
                        . $this->Html->link("View Profile", "/company/view/" . $row["Company"]["id"], array("class" => "btn btn-primary"))
                        . '</div>';

            endforeach;

            break;

        case 3:
            $name_type = 'Analyst';
            foreach ($rows as $row) :
                $current_value = $previous_value = 0;
                if ($row['Stock']['values']) {
                    $current_value = $row['Stock']['values'][0]['StockValues']['closing'];
                    if (isset($row['Stock']['values'][1])) {
                        $previous_value = $row['Stock']['values'][1]['StockValues']['closing'];
                    }
                    if ($current_value != $previous_value) {
                        $text_display .='<div class="services-box services-bgcolor e_bounce">'
                                . '<h5>' . $row['Broker']['fullname'] . '</h5>'
                                . '<h5 style="color: #e55237;">' . $row['Stock']['name'] . '</h5>'
                                . '<p>Curent Value = ' . $current_value . '<br>On ' . date('d/m/Y', strtotime($row['Stock']['values'][1]['StockValues']['date'])) . ' = ' . $previous_value . '<br></p>'
                                . '<p>Previous Difference<br><i style="color:#0C3;">True</i></p>'
                                . $this->Html->link("View Profile", "/analyst/view/" . $row["Broker"]["id"], array("class" => "btn btn-primary"))
                                . '</div>';
                    } else {
                        $text_display .='<div class="services-box services-bg1 e_bounce">'
                                . '<h5>' . $row['Broker']['fullname'] . '</h5>'
                                . '<h5 style="color: #e55237;">' . $row['Stock']['name'] . '</h5>'
                                . '<p>Curent Value = ' . $current_value . '<br>On ' . date('d/m/Y', strtotime($row['Stock']['values'][1]['StockValues']['date'])) . ' = ' . $previous_value . '<br></p>'
                                . '<p>Previous Difference<br><i style="color:#C33;">Wrong</i></p>'
                                . $this->Html->link("View Profile", "/analyst/view/" . $row["Broker"]["id"], array("class" => "btn btn-primary"))
                                . '</div>';
                    }
                } else {
                    $text_display .='<div class="services-box services-bg1 e_bounce">'
                            . '<h5>' . $row['Broker']['fullname'] . '</h5>'
                            . '<h5 style="color: #e55237;">' . $row['Stock']['name'] . '</h5>'
                            . '<p>&nbsp;</p>'
                            . $this->Html->link("View Profile", "/analyst/view/" . $row["Broker"]["id"], array("class" => "btn btn-primary"))
                            . '</div>';
                }

            endforeach;

            break;

        default:
            $name_type = 'Performer';
            foreach ($rows as $row) {
                $right = $wrong = 0;
                if ($row['Broker']['opinions']) {
                    $total_opinions = sizeof($row['Broker']['opinions']);
                    $total_right = 0;
                    foreach ($row['Broker']['opinions'] as $opinion) {
                        if ($opinion['Opinion']['result'] == 1) {
                            $total_right++;
                        }
                    }

                    $right = number_format(($total_right / $total_opinions) * 100);
                    $wrong = 100 - $right;
                }
                $text_display .='<div class="services-box services-bg1 e_bounce">'                        
                            .'<h5>'.$row['Broker']['fullname'].'</h5>' 
                            .'<p>Performance <br><br>' 
                                .'<span id="green" style="display: block; width:'.$right.'%; background-color:#093;">'.$right.'%</span>' 
                                .'<span id="red" style="display: block; width:'.$wrong.'%; background-color:rgba(204,51,0,1);">'.$wrong.'%</span>' 
                            .'</p>' 
                            .$this->Html->link('View Profile', '/analyst/view/' . $row['Broker']['id'], array('class' => 'btn btn-primary'))
                        .'</div>';
            }
            break;
    }
    ?>
    <section id="Analyst">
        <div class="page-head">
            <div class="container">
                <div class="row">
                    <div class="span9 head-text">
                        <h3>List of <span><?php echo $name_type; ?></span></h3>                    
                    </div>
                    <div class="span3">
                        <i class="icon icon-play-sign"></i>
                    </div>						
                </div>
            </div>			
        </div>
        <div class="container">
            <div class="page-section">
                <div class="row">
                    <div class="span12 aligncenter">
                        <?php echo $text_display; ?>      

                        <div class="span12 pagination">
                            &nbsp;
                            <ul>
                                <?php
                                if ($paginator->hasPrev()) {
                                    echo '<li>' . $paginator->prev("Prev") . '</li>';
                                }
                                ?>
                                <?php echo $paginator->numbers(array('modulus' => 3, 'tag' => 'li', 'separator' => '', 'currentTag' => 'a', 'currentClass' => 'active')); ?>
                                <?php
                                if ($paginator->hasNext()) {
                                    echo '<li>' . $paginator->next("Next") . '</li>';
                                }
                                ?>
                            </ul>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </section>

    <?php
} else {
    ?>
    <section id="Analyst">
        <div class="page-head">
            <div class="container">
                <div class="row">
                    <div class="span9 head-text">
                        <h3>DID NOT MATCH ANY DATA.</h3>                    
                    </div>
                    <div class="span3">
                        <i class="icon icon-play-sign"></i>
                    </div>						
                </div>
            </div>			
        </div>    
    </section>
<?php }
?>